home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / demo / pwrtcp11.exe / FTPC.C_ / FTPC.bin
Text File  |  1995-02-17  |  50KB  |  1,763 lines

  1. #pragma warning(disable:4001)
  2. //***************************************************************************
  3. //
  4. //  Module: ftptest.c
  5. //
  6. //    Tab setting: 4
  7. //
  8. //***************************************************************************
  9. //
  10. //  Written by Dart Communication Application Programming Group.
  11. //  Copyright (c) 1994 Dart Communications.  All Rights Reserved.
  12. //
  13. //  Purpose: Provides access to functions in FTP interface
  14. //             DLL via dialog box.
  15. //
  16. //    Author:  Roger Lathrop
  17. //
  18. //    History: 11/94 - First version.
  19. //
  20. //***************************************************************************
  21.  
  22. #include <malloc.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26.  
  27. // FTP Include files. Common brings in windows.h for us.
  28. #include "..\..\include\common.h"
  29. #include "..\..\include\powertcp.h"
  30. //#include "powerftp.h"
  31.  
  32. // Our resource file header.
  33. #include "ftprc.h"
  34.  
  35.  
  36. /****************************************************************************
  37. * Static variables for module.
  38. ****************************************************************************/
  39. static HICON     hOurIcon;        // Handle to our application icon.
  40.  
  41. static HPOWERTCP hFtp;            // Handle to PowerFTP session.
  42. static BOOL      ClosePending;  // Close app when logout complete.
  43.  
  44. static HFILE hSendFile = HFILE_ERROR;  // File handle for sending data.
  45. static HFILE hRecvFile = HFILE_ERROR;  // File handle for receiving data.
  46.  
  47. static DWORD BytesSent;            // Accumulated bytes sent per transfer.
  48. static DWORD BytesReceived;        // Accumulated bytes received per transfer.
  49.  
  50. static CONNECTEVENT lpfnConnectEvent;
  51. static LOGEVENT     lpfnLogEvent;
  52. static RECVEVENT     lpfnRecvEvent;
  53. static REPLYEVENT     lpfnReplyEvent;
  54. static SENDEVENT     lpfnSendEvent;
  55.  
  56. // A single, static buffer is allocate here for sending data.
  57. #define SEND_BUFFER_SIZE        4096
  58. static char SendBuffer[SEND_BUFFER_SIZE];
  59.  
  60. /****************************************************************************
  61. * Routines for fetching parameters from edit controls.
  62. ****************************************************************************/
  63. //---------------------------------------------------------------------------
  64. // Name.......: GetStringParam
  65. // Purpose....: Get a string from an edit control.
  66. //
  67. // Parameters.: hWnd    - HWND Main window handle.
  68. //                CtlId    - WORD Control id of edit control
  69. //              Required - BOOL Parameter is required.
  70. //
  71. // Returns....: Malloced pointer to string, or NULL if malloc fails
  72. //---------------------------------------------------------------------------
  73. static char * GetStringParam(HWND hWnd,WORD CtlId,BOOL Required)
  74.     { char *p;
  75.       WORD len;
  76.  
  77.     // Get length of string in edit control. If length is 0,
  78.     // and the parameter is required, then ding the speaker,
  79.     // set focus to control, and put message in status bar.
  80.     len = (WORD)SendDlgItemMessage(hWnd,CtlId,WM_GETTEXTLENGTH,0,0);
  81.     if(len == 0 && Required)
  82.         {
  83.         MessageBeep(0);
  84.         SetFocus(GetDlgItem(hWnd,CtlId));
  85.         SetDlgItemText(hWnd,STC_STATUS,"Parameter required.");
  86.         return NULL;
  87.         }
  88.  
  89.     // Allocate space for string and a NULL byte.
  90.     len++;
  91.     p = malloc(len);
  92.  
  93.     // Malloc should never fail, unless we have a memory leak,
  94.     // but best to check anyway.
  95.     if(p)
  96.         { // Extract string from edit control.
  97.         SendDlgItemMessage(hWnd,CtlId,WM_GETTEXT,len,(LPARAM)(LPSTR)p);
  98.         }
  99.  
  100.     return p;
  101.     }
  102.  
  103. //---------------------------------------------------------------------------
  104. // Name.......: FreeStringParam
  105. // Purpose....: _ffree a string allocated by GetStringParam.
  106. //                Checks for NULL string so caller doesn't have to.
  107. //
  108. // Parameters.: s    - Char * String from GetStringParam
  109. //
  110. // Returns....: void
  111. //---------------------------------------------------------------------------
  112. static void FreeStringParam(char *s)
  113.     {
  114.     if(s)
  115.         {
  116.         free(s);
  117.         }
  118.     }
  119.  
  120. //---------------------------------------------------------------------------
  121. // Name.......: GetLongParam
  122. // Purpose....: Get a long int from an edit control.
  123. //
  124. // Parameters.: hWnd    - HWND Main window handle.
  125. //                CtlId    - WORD Control id of edit control
  126. //                pval    - LPDWORD far pointer to area to receive value.
  127. //
  128. // Returns....: True if numeric value in edit control.
  129. //---------------------------------------------------------------------------
  130. BOOL GetLongParam(HWND hWnd,WORD CtlId,LPDWORD pval)
  131.     { char *p;
  132.       char *psave;
  133.  
  134.     // Get text from edit control. Skip leading whitespace, then
  135.     // if first character is a digit, convert string to long.
  136.     // If not a digit, ding the speaker and set focus to control.
  137.     p = GetStringParam(hWnd,CtlId,TRUE);
  138.     psave = p;
  139.     if(p)
  140.         {
  141.         while(isspace(*p))
  142.             {
  143.             p++;
  144.             }
  145.  
  146.         if(isdigit(*p))
  147.             {
  148.             *pval = atol(p);
  149.             free(psave);
  150.             return TRUE;
  151.             }
  152.         else
  153.             {
  154.             MessageBeep(0);
  155.             SetFocus(GetDlgItem(hWnd,CtlId));
  156.             SetDlgItemText(hWnd,STC_STATUS,"Numeric value required.");
  157.             free(psave);
  158.             return FALSE;
  159.             }
  160.         }
  161.  
  162.     return FALSE;
  163.     }
  164.  
  165. //---------------------------------------------------------------------------
  166. // Name.......: ShowResult
  167. // Purpose....: Print Success or Failed in status line
  168. //
  169. // Parameters.: hWnd        -   Our window handle.
  170. //                Success        -  BOOL True for success
  171. //
  172. // Returns....: void
  173. //---------------------------------------------------------------------------
  174. static void ShowResult(HWND hWnd,BOOL Success)
  175.     {
  176.     // Diagnose the obvios case for a failure...
  177.     if(!Success && hFtp == NULL)
  178.         {
  179.         SetDlgItemText(hWnd,STC_STATUS,"Failed - Not Logged on.");
  180.         }
  181.     else
  182.         {
  183.         SetDlgItemText(hWnd,STC_STATUS,Success ? "Success" : "Failed");
  184.         }
  185.     }
  186.  
  187. /****************************************************************************
  188. * Routines to kick off or abort a send or receive request.
  189. ****************************************************************************/
  190.  
  191. //---------------------------------------------------------------------------
  192. // Name.......: AbortSend
  193. // Purpose....: If sending from file, close it.
  194. //
  195. // Parameters.: void
  196. //
  197. // Returns....: void
  198. //---------------------------------------------------------------------------
  199. static void AbortSend(void)
  200.     {
  201.     if(hSendFile != HFILE_ERROR)
  202.         {
  203.         _lclose(hSendFile);
  204.         hSendFile = HFILE_ERROR;
  205.         }
  206.     }
  207.  
  208. //---------------------------------------------------------------------------
  209. // Name.......: AbortRecv
  210. // Purpose....: If receiving into file, close it.
  211. //
  212. // Parameters.: void
  213. //
  214. // Returns....: void
  215. //---------------------------------------------------------------------------
  216. static void AbortRecv(void)
  217.     {
  218.     if(hRecvFile != HFILE_ERROR)
  219.         {
  220.         _lclose(hRecvFile);
  221.         hRecvFile = HFILE_ERROR;
  222.         }
  223.     }
  224.  
  225. //---------------------------------------------------------------------------
  226. // Name.......: StartSend
  227. // Purpose....: Prepare to send a file, if it is safe to do so.
  228. //
  229. // Parameters.: hWnd        -   Our window handle.
  230. //                LocalName    -    LPSTR of local file name to send.
  231. //
  232. // Returns....: True if ok to send.
  233. //---------------------------------------------------------------------------
  234. static BOOL StartSend(HWND hWnd,LPSTR LocalName)
  235.     { int res;
  236.       OFSTRUCT ofsFile;
  237.  
  238.     // If we are already sending a file, then hSendFile will be
  239.     // valid. In this case, ask the user what to do.
  240.     // Abort? - Close file, send AbortFtp, and start new send.
  241.     // Retry? - See if previous transfer has completed yet.
  242.     // Ignore? - Cancel request to send.
  243.     while(hSendFile != HFILE_ERROR)
  244.         {
  245.         res = MessageBox(hWnd,
  246.                            "A transfer is already in progress.\n"
  247.                             "Abort aborts active transfer.\n"
  248.                             "Retry proceeds if transfer has completed.\n"
  249.                             "Ignore cancels your request.\n",
  250.                             "FTPTest",
  251.                             MB_ICONSTOP | MB_ABORTRETRYIGNORE);
  252.         switch(res)
  253.             {
  254.             case IDABORT:  AbortSend();
  255.                            AbortFtp(hFtp);
  256.                            break;
  257.             case IDRETRY:  continue;
  258.             case IDIGNORE: return FALSE;
  259.             }
  260.         }
  261.  
  262.     // Ok to attempt send. See if we can open the file.
  263.     ofsFile.cBytes = sizeof ofsFile;
  264.     hSendFile     = OpenFile(LocalName,&ofsFile,OF_READ | OF_SHARE_COMPAT);
  265.     if(hSendFile == HFILE_ERROR)
  266.         {
  267.         MessageBox(hWnd,"Unable to open file","FTPTest",MB_ICONSTOP | MB_OK);
  268.         return FALSE;
  269.         }
  270.     // Reset stats for transfer
  271.     BytesSent = 0;
  272.     return TRUE;
  273.     }
  274.  
  275.  
  276. //---------------------------------------------------------------------------
  277. // Name.......: StartReceive
  278. // Purpose....: Prepare to receive a file.
  279. //
  280. // Parameters.: hWnd        -  Our window handle.
  281. //                LocalName    -  LPSTR of local file to receive into.
  282. //                               If NULL, then receive into edit window.
  283. //
  284. // Returns....: True if ok to send.
  285. //---------------------------------------------------------------------------
  286. static BOOL StartReceive(HWND hWnd,char *LocalName)
  287.     { int res;
  288.       OFSTRUCT ofsFile;
  289.  
  290.     // If LocalName is provided, then we receive into a file,
  291.     // otherwise we receive into the edit box.
  292.     if(LocalName)
  293.         {
  294.         // If we are already receiving a file, then hRecvFile will be
  295.         // valid. In this case, ask the user what to do.
  296.         // Abort? - Close file, send AbortFtp, and start new recv.
  297.         // Retry? - See if previous transfer has completed yet.
  298.         // Ignore? - Cancel request to receive.
  299.         while(hRecvFile != HFILE_ERROR)
  300.             {
  301.             res = MessageBox(hWnd,
  302.                                "A transfer is already in progress.\n"
  303.                                 "Abort aborts active transfer.\n"
  304.                                 "Retry proceeds if transfer has completed.\n"
  305.                                 "Ignore cancels your request.\n",
  306.                                 "FTPTest",
  307.                                 MB_ICONSTOP | MB_ABORTRETRYIGNORE);
  308.             switch(res)
  309.                 {
  310.                 case IDABORT:  AbortRecv();
  311.                                   AbortFtp(hFtp);
  312.                                   break;
  313.                 case IDRETRY:  continue;
  314.                 case IDIGNORE: return FALSE;
  315.                 }
  316.             }
  317.         // Ok to receive, see if we can open the file to receive into.
  318.         // NB: This action truncates the file if it exists without warning!
  319.         ofsFile.cBytes = sizeof ofsFile;
  320.         hRecvFile      = OpenFile(LocalName,
  321.                                   &ofsFile,
  322.                                   OF_WRITE | OF_CREATE);
  323.         if(hRecvFile == HFILE_ERROR)
  324.             {
  325.             MessageBox(hWnd,"Unable to open file","FTPTest",MB_ICONSTOP | MB_OK);
  326.             return FALSE;
  327.             }
  328.         }
  329.     else
  330.         {
  331.         // We are going to receive into the edit control. Clear it first.
  332.         SendDlgItemMessage(hWnd,
  333.                            EDC_RECEIVE,
  334.                            WM_SETTEXT,
  335.                            0,
  336.                            (LPARAM)(LPSTR)"");
  337.  
  338.         }
  339.     // Reset stats for transfer
  340.     BytesReceived = 0;
  341.     return TRUE;
  342.     }
  343.  
  344. /****************************************************************************
  345. * CALLBACK FUNCTIONS. Called asynchronously by DLL as events occur.
  346. ****************************************************************************/
  347.  
  348. //---------------------------------------------------------------------------
  349. // Name.......: ConnectEvent
  350. // Purpose....: Callback function for connect event notification
  351. //
  352. // Parameters.: hSession        - HPOWERTCP Session id.
  353. //                UserData        - DWORD     User data supplied to loginhost.
  354. //                RemoteDotAddr    - LPCSTR Remote dot address
  355. //                RemotePort        - WORD   Remote port id.
  356. //                LocalDotAddr    - LPCSTR Local dot address
  357. //                LocalPort        - WORD   Local port id.
  358. //                LocalName        - LPCSTR Local name.
  359. //
  360. // Returns....: void
  361. //---------------------------------------------------------------------------
  362. void CALLBACK ConnectEvent(HPOWERTCP hSession,
  363.                                         DWORD    UserData,
  364.                                         LPCSTR     RemoteDotAddr,
  365.                                         WORD       RemotePort,
  366.                                         LPCSTR     LocalDotAddr,
  367.                                         WORD       LocalPort,
  368.                                         LPCSTR     LocalName)
  369.     { int len;
  370.       static char msg[] = "Connected to:";
  371.       char *txt;
  372.  
  373.     // This callback is invoked when we connect end-to-end. Put
  374.     // the connected to dot address in status box.
  375.     len = RemoteDotAddr ? _fstrlen(RemoteDotAddr) : 0;
  376.     len += sizeof(msg)+1;
  377.     txt = malloc(len);
  378.  
  379.     if(!txt)
  380.         {
  381.         return;
  382.         }
  383.  
  384.     strcpy(txt,msg);
  385.     if(RemoteDotAddr)
  386.         {
  387.         _fstrcat(txt,RemoteDotAddr);
  388.         }
  389.     SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,txt);
  390.     free(txt);
  391.     }
  392.  
  393. //---------------------------------------------------------------------------
  394. // Name.......: LogEvent
  395. // Purpose....: Callback function for log event notification
  396. //
  397. // Parameters.: hSession    - HPOWERTCP Session id.
  398. //                UserData    - DWORD     User data supplied to loginhost.
  399. //                 Message        - LPCSTR to log event message
  400. //
  401. // Returns....: void
  402. //---------------------------------------------------------------------------
  403. void CALLBACK LogEvent(HPOWERTCP hSession,
  404.                                DWORD UserData,
  405.                                LPCSTR Message)
  406.     {
  407.     // Just plop the string in the Log list box control.
  408.     SendDlgItemMessage((HWND)(WORD)UserData,LBC_LOG,LB_ADDSTRING
  409.                        ,0,(LPARAM)Message);
  410.     }
  411.  
  412. //---------------------------------------------------------------------------
  413. // Name.......: RecvEvent
  414. // Purpose....: Callback function for received data notification
  415. //
  416. // Parameters.: Data    - LPVOID pointer to data
  417. //                ByteCnt    - size_t number of bytes in Data
  418. //
  419. // Returns....: void
  420. //---------------------------------------------------------------------------
  421. void CALLBACK RecvEvent(HPOWERTCP hSession,
  422.                                 DWORD UserData,
  423.                                 LPVOID Data,
  424.                                 size_t ByteCnt)
  425.     { LPSTR p;
  426.       char Status[64];
  427.       UINT Written;
  428.  
  429.     // This callback function is invoked when data arrives for us
  430.     // from remote station, or to indicate the data connection
  431.     // has been closed. Check ByteCnt to determine which case this is.
  432.     if(ByteCnt)
  433.         {
  434.  
  435.         BytesReceived += ByteCnt;
  436.  
  437.         // We may be receiving data into edit control or file. If hRecvFile
  438.         // file handle is valid, then write data to file.
  439.         if(hRecvFile != HFILE_ERROR)
  440.             {
  441.             Written = _lwrite(hRecvFile,Data,ByteCnt);
  442.             if(Written != ByteCnt)
  443.                 {
  444.                 wsprintf(Status,"%ld bytes received. Error Writing File.",
  445.                          BytesReceived);
  446.                 SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  447.                 return;
  448.                 }
  449.                 else
  450.                 {
  451.                 // Update status line to reflect DONE.
  452.                 wsprintf(Status,"%ld bytes received.",
  453.                          BytesReceived);
  454.                 SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  455.                 
  456.                 }
  457.             }
  458.         else
  459.             {
  460.             // Write data to edit control. Data WILL NOT contain
  461.             // a terminating NULL, so duplicate the data, add
  462.             // a NULL byte, then write to edit control. If the
  463.             // alloc fails, we silently drop the data.
  464. //MJB            p = _fmalloc(ByteCnt+1);
  465.             p = (LPSTR)malloc(ByteCnt+1);
  466.             if(p)
  467.                 {
  468.                 _fmemcpy(p,Data,ByteCnt);
  469.                 p[ByteCnt] = '\0';
  470.                 SendDlgItemMessage((HWND)(WORD)UserData,
  471.                                          EDC_RECEIVE,
  472.                                          EM_SETSEL,
  473.                                          TRUE,
  474.                                          MAKELPARAM(-1,-1));
  475.                 SendDlgItemMessage((HWND)(WORD)UserData,
  476.                                          EDC_RECEIVE,
  477.                                          EM_REPLACESEL,
  478.                                          0,
  479.                                          (LPARAM)p);
  480. // MJB            _ffree(p);
  481.                 free((char *)p);
  482.                 }
  483.             }
  484.         }
  485.     else
  486.         {
  487.         // Data transfer complete. If receiving into file, close
  488.         // the file now. Reset hRecvFile handle.
  489.         if(hRecvFile != HFILE_ERROR)
  490.             {
  491.             _lclose(hRecvFile);
  492.             hRecvFile = HFILE_ERROR;
  493.             // Update status line to reflect DONE.
  494.             wsprintf(Status,"%ld bytes received. DONE.",
  495.                      BytesReceived);
  496.             SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  497.             }
  498.  
  499.         BytesReceived = 0;
  500.         }
  501.     }
  502.  
  503.  
  504. //---------------------------------------------------------------------------
  505. // Name.......: ReplyEvent
  506. // Purpose....: Callback function for reply event notification
  507. //
  508. // Parameters.: hSession    - HPOWERTCP Session id.
  509. //                UserData    - DWORD     User data supplied to loginhost.
  510. //                 Status        - FTP_STATUS, status of ftp dialog (enumerated type)
  511. //                LastCommand - FTP_COMMAND last ftp command (enumerated type)
  512. //                ReplyCode    - int ftp protocol return code
  513. //                ReplyStr    - LPCSTR reply string
  514. //
  515. // Returns....: void
  516. //---------------------------------------------------------------------------
  517. void CALLBACK ReplyEvent(HPOWERTCP hSession,
  518.                                   DWORD       UserData,
  519.                                   FTP_STATUS Status,
  520.                                   FTP_COMMAND LastCommand,
  521.                                      int ReplyCode,
  522.                                   LPCSTR ReplyStr)
  523.     { LPSTR s;
  524.       LPSTR token;
  525.  
  526.     // ReplyEvent notifications may have embedded <cr><lf>'s. Since
  527.     // we are dropping the text in an list box, we need to break
  528.     // the string into multiple pieces. The strtok function does
  529.     // just what we need. First we duplicate the string, since
  530.     // strtok modifies the passed in string, and ReplyStr doesn't
  531.     // really 'belong' to us.
  532.     s = _fstrdup(ReplyStr);
  533.     if(s)
  534.         {
  535.         token = _fstrtok(s,(LPSTR)"\r\n");
  536.         while(token)
  537.             {
  538.             SendDlgItemMessage((HWND)(WORD)UserData,LBC_REPLIES,LB_ADDSTRING
  539.                                   ,0,(LPARAM)token);
  540.             token = _fstrtok(NULL,"\r\n");
  541.             }
  542.         _ffree(s);
  543.         }
  544.     else
  545.         {
  546.         // In the unlikely event that _fstrdup failed, drop the
  547.         // entire string into the list box.
  548.         SendDlgItemMessage((HWND)(WORD)UserData,LBC_REPLIES,LB_ADDSTRING
  549.                               ,0,(LPARAM)ReplyStr);
  550.         }
  551.  
  552.     // If connection is closed, then hFtp is no longer valid.
  553.     if(LastCommand == FTP_CLOSED)
  554.         {
  555.         AbortSend(); // Cleanup just in case.
  556.         AbortRecv();
  557.  
  558.         hFtp = NULL;
  559.         SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,"Closed");
  560.         // Close app when connection closed? (See WM_SYSCOMMAND).
  561.         if(ClosePending)
  562.             {
  563.             ClosePending = FALSE;
  564.               PostQuitMessage(0);
  565.             }
  566.         }
  567.     }
  568.  
  569. //---------------------------------------------------------------------------
  570. // Name.......: SendEvent
  571. // Purpose....: Callback function for send event notification
  572. //
  573. // Parameters.: hSession    - HPOWERTCP Session id.
  574. //                UserData    - DWORD    User data supplied to loginhost.
  575. //                 Tag            - DWORD Tag, Not used by FTP
  576. //
  577. // Returns....: void
  578. //---------------------------------------------------------------------------
  579. void CALLBACK SendEvent(HPOWERTCP hSession,DWORD UserData,DWORD Tag)
  580.     { UINT Bytes;
  581.       char Status[40];
  582.  
  583.     // This callback is invoked when data we have sent has been
  584.     // accepted by the transport layer. We can now send the
  585.     // next chunk.
  586.     if(hSendFile != HFILE_ERROR)
  587.         {
  588.         // Read next chunk from file.
  589.         Bytes = _lread(hSendFile,SendBuffer,SEND_BUFFER_SIZE);
  590.         if((HFILE)Bytes == HFILE_ERROR)
  591.             { // Error reading file, close file and data connection,
  592.               // post error message in status line.
  593.             AbortSend();
  594.             CloseDataFtp(hSession);
  595.             wsprintf(Status,"%ld bytes sent. Error Reading File.",BytesSent);
  596.             SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  597.             return; // No more data to send, return.
  598.             }
  599.  
  600.         // If we have reached end of file, close the file and
  601.         // the data connection. Indicate DONE in status line.
  602.         if(Bytes == 0)
  603.             {
  604.             _lclose(hSendFile);
  605.             hSendFile = HFILE_ERROR;
  606.             CloseDataFtp(hSession);
  607.             wsprintf(Status,"%ld bytes sent. DONE.",BytesSent);
  608.             SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  609.             return; // No more data to send, return.
  610.             }
  611.  
  612.         // Send this block, update statistics and status line.
  613.         SendFtp(hSession,SendBuffer,Bytes);
  614.         BytesSent += Bytes;
  615.         wsprintf(Status,"%ld bytes sent.",BytesSent);
  616.         SetDlgItemText((HWND)(WORD)UserData,STC_STATUS,Status);
  617.         }
  618.     }
  619.  
  620. /****************************************************************************
  621. * ActionXxx Functions. Handle user actions, dispatched from MainWndProc.
  622. *
  623. * All functions follow the same general format:
  624. * 1) Get parameters from edit controls.
  625. * 2) Call xxxFtp(parameters).
  626. * 3) Display results of call.
  627. * 4) _ffree any strings allocated for parameters.
  628. *
  629. ****************************************************************************/
  630.  
  631. //---------------------------------------------------------------------------
  632. // Name.......: ActionAbort
  633. // Purpose....: Handle BTN_ABORT command.
  634. //---------------------------------------------------------------------------
  635. static void ActionAbort(HWND hWnd)
  636.     { BOOL res;
  637.  
  638.     // Closedown files we may be sending/receiving
  639.     AbortSend();
  640.     AbortRecv();
  641.  
  642.     res = AbortFtp(hFtp);
  643.     ShowResult(hWnd,res);
  644.     }
  645.  
  646. //---------------------------------------------------------------------------
  647. // Name.......: ActionAllocate
  648. // Purpose....: Handle BTN_ALLOCATE command.
  649. //---------------------------------------------------------------------------
  650. static void ActionAllocate(HWND hWnd)
  651.     { LONG MaxFileSize;
  652.       LONG MaxRecordSize;
  653.       BOOL res;
  654.  
  655.     if(  GetLongParam(hWnd,EDC_PARAM1,(LPDWORD)&MaxFileSize)
  656.        &&
  657.          GetLongParam(hWnd,EDC_PARAM2,(LPDWORD)&MaxRecordSize))
  658.         {
  659.         res = AllocateFtp(hFtp,MaxFileSize,MaxRecordSize);
  660.         ShowResult(hWnd,res);
  661.         }
  662.     }
  663.  
  664. //---------------------------------------------------------------------------
  665. // Name.......: ActionAppe
  666. // Purpose....: Handle BTN_APPE command.
  667. //---------------------------------------------------------------------------
  668. static void ActionAppe(HWND hWnd)
  669.     { char *PathName;
  670.       char *LocalName;
  671.       BOOL res;
  672.  
  673.     LocalName = GetStringParam(hWnd,EDC_PARAM2,TRUE);
  674.     PathName  = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  675.  
  676.     if(PathName && LocalName)
  677.         {
  678.         if(StartSend(hWnd,LocalName))
  679.             {
  680.             res = AppeFtp(hFtp,PathName);
  681.             if(!res)
  682.                 {
  683.                 AbortSend();
  684.                 }
  685.             ShowResult(hWnd,res);
  686.             }
  687.         }
  688.  
  689.     FreeStringParam(PathName);
  690.     FreeStringParam(LocalName);
  691.     }
  692.  
  693. //---------------------------------------------------------------------------
  694. // Name.......: ActionChDir
  695. // Purpose....: Handle BTN_CHDIR command.
  696. //---------------------------------------------------------------------------
  697. static void ActionChDir(HWND hWnd)
  698.     { char *PathName;
  699.       BOOL res;
  700.  
  701.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  702.  
  703.     if(PathName)
  704.         {
  705.         res = ChDirFtp(hFtp,PathName);
  706.         ShowResult(hWnd,res);
  707.         FreeStringParam(PathName);
  708.         }
  709.     }
  710.  
  711. //---------------------------------------------------------------------------
  712. // Name.......: ActionChDirUp
  713. // Purpose....: Handle BTN_CHDIRUP command.
  714. //---------------------------------------------------------------------------
  715. static void ActionChDirUp(HWND hWnd)
  716.     { BOOL res;
  717.     res = ChDirUpFtp(hFtp);
  718.     ShowResult(hWnd,res);
  719.     }
  720.  
  721. //---------------------------------------------------------------------------
  722. // Name.......: ActionClose
  723. // Purpose....: Handle BTN_CLOSE command.
  724. //---------------------------------------------------------------------------
  725. static void ActionClose(HWND hWnd)
  726.     { BOOL res;
  727.  
  728.     // Closedown files we may be sending/receiving
  729.     AbortSend();
  730.     AbortRecv();
  731.  
  732.     res = CloseFtp(hFtp);
  733.     ShowResult(hWnd,res);
  734.     }
  735.  
  736. //---------------------------------------------------------------------------
  737. // Name.......: ActionCloseData
  738. // Purpose....: Handle BTN_CLOSEDATA command.
  739. //---------------------------------------------------------------------------
  740. static void ActionCloseData(HWND hWnd)
  741.     { BOOL res;
  742.  
  743.     // Closedown files we may be sending/receiving
  744.     AbortSend();
  745.     AbortRecv();
  746.  
  747.     res = CloseDataFtp(hFtp);
  748.     ShowResult(hWnd,res);
  749.     }
  750.  
  751. //---------------------------------------------------------------------------
  752. // Name.......: ActionCommand
  753. // Purpose....: Handle BTN_COMMAND command.
  754. //---------------------------------------------------------------------------
  755. static void ActionCommand(HWND hWnd)
  756.     { char *CommandStr;
  757.       BOOL res;
  758.  
  759.     CommandStr = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  760.  
  761.     if(CommandStr)
  762.         {
  763.         res = CommandFtp(hFtp,CommandStr);
  764.         ShowResult(hWnd,res);
  765.         FreeStringParam(CommandStr);
  766.         }
  767.     }
  768.  
  769. //---------------------------------------------------------------------------
  770. // Name.......: ActionDelete
  771. // Purpose....: Handle BTN_DELETE command.
  772. //---------------------------------------------------------------------------
  773. static void ActionDelete(HWND hWnd)
  774.     { char *PathName;
  775.       BOOL res;
  776.  
  777.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  778.  
  779.     if(PathName)
  780.         {
  781.         res = DeleteFtp(hFtp,PathName);
  782.         ShowResult(hWnd,res);
  783.         FreeStringParam(PathName);
  784.         }
  785.     }
  786.  
  787. //---------------------------------------------------------------------------
  788. // Name.......: ActionFileStruct
  789. // Purpose....: Handle BTN_FILESTRUCT command.
  790. //---------------------------------------------------------------------------
  791. static void ActionFileStruct(HWND hWnd)
  792.     {  BOOL res;
  793.        char *StructName;
  794.        FTP_FILE_STRUCT Structure;
  795.  
  796.  
  797.     StructName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  798.     if(StructName)
  799.         {
  800.         if( stricmp(StructName,"FTP_FILE")==0 ||
  801.             stricmp(StructName,"FILE")==0)
  802.             {
  803.             Structure = FTP_FILE;
  804.             }
  805.         else if( stricmp(StructName,"FTP_RECORD") == 0 ||
  806.                  stricmp(StructName,"RECORD") == 0)
  807.             {
  808.             Structure = FTP_RECORD;
  809.             }
  810.         else if( stricmp(StructName,"FTP_PAGE") == 0 ||
  811.                  stricmp(StructName,"PAGE") == 0)
  812.             {
  813.             Structure = FTP_PAGE;
  814.             }
  815.         else
  816.             {
  817.             FreeStringParam(StructName);
  818.             SetDlgItemText(hWnd,STC_STATUS,"Enter: file,record or page");
  819.             return;
  820.             }
  821.  
  822.         res = FileStructFtp(hFtp,Structure);
  823.         ShowResult(hWnd,res);
  824.         FreeStringParam(StructName);
  825.         }
  826.     }
  827.  
  828. //---------------------------------------------------------------------------
  829. // Name.......: ActionHelp
  830. // Purpose....: Handle BTN_HELP command.
  831. //---------------------------------------------------------------------------
  832. static void ActionHelp(HWND hWnd)
  833.     { char *Command;
  834.       BOOL res;
  835.     Command = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  836.  
  837.     if(Command)
  838.         {
  839.         res = HelpFtp(hFtp,Command);
  840.         ShowResult(hWnd,res);
  841.         FreeStringParam(Command);
  842.         }
  843.     }
  844.  
  845. //---------------------------------------------------------------------------
  846. // Name.......: ActionLastCommand
  847. // Purpose....: Handle BTN_LASTCOMMAND command.
  848. //---------------------------------------------------------------------------
  849. static void ActionLastCommand(HWND hWnd)
  850.     { BOOL res;
  851.     res = LastCommandFtp(hFtp);
  852.     ShowResult(hWnd,res);
  853.     }
  854.  
  855. //---------------------------------------------------------------------------
  856. // Name.......: ActionList
  857. // Purpose....: Handle BTN_LIST command.
  858. //---------------------------------------------------------------------------
  859. static void ActionList(HWND hWnd)
  860.     { char *PathName;
  861.       BOOL res;
  862.  
  863.     PathName = GetStringParam(hWnd,EDC_PARAM1,FALSE);
  864.  
  865.     if(PathName)
  866.         {
  867.         if(StartReceive(hWnd,NULL))
  868.         {
  869.             res = ListFtp(hFtp,PathName);
  870.             if(!res)
  871.                 {
  872.                 AbortRecv();
  873.                 }
  874.             ShowResult(hWnd,res);
  875.         }
  876.         FreeStringParam(PathName);
  877.         }
  878.     }
  879.  
  880. //---------------------------------------------------------------------------
  881. // Name.......: ActionLoginHost
  882. // Purpose....: Handle BTN_LOGINHOST command.
  883. //---------------------------------------------------------------------------
  884. static void ActionLoginHost(HWND hWnd)
  885.     { char *HostName;
  886.       char *UserName;
  887.       char *Password;
  888.       char *Account;
  889.       char *License;
  890.       char Status[32];
  891.  
  892.     UserName = GetStringParam(hWnd,EDC_USER,TRUE);
  893.     HostName = GetStringParam(hWnd,EDC_HOST,TRUE);
  894.     Password = GetStringParam(hWnd,EDC_PASSWORD,FALSE);
  895.     Account  = GetStringParam(hWnd,EDC_ACCOUNT,FALSE);
  896.     License  = GetStringParam(hWnd,EDC_LICENSE,FALSE);
  897.  
  898.     if(HostName && UserName)
  899.         {
  900.         hFtp = LoginHostFtp((DWORD)MAKELONG(hWnd,0),
  901.                             License,
  902.                             PT_DEBUG,
  903.                             HostName,
  904.                             NULL,
  905.                             UserName,
  906.                             Password,
  907.                             Account,
  908.                             lpfnConnectEvent,
  909.                             lpfnLogEvent,
  910.                             lpfnRecvEvent,
  911.                             lpfnReplyEvent,
  912.                             lpfnSendEvent);
  913.         if(hFtp)
  914.             {
  915.             wsprintf(Status,"Login OK. Handle=%x",hFtp);
  916.             SetDlgItemText(hWnd,STC_STATUS,Status);
  917.             }
  918.         else
  919.             {
  920.             ShowResult(hWnd,FALSE);
  921.             }
  922.         }
  923.  
  924.  
  925.     FreeStringParam(HostName);
  926.     FreeStringParam(UserName);
  927.     FreeStringParam(Password);
  928.     FreeStringParam(Account);
  929.     FreeStringParam(License);
  930.  
  931.     }
  932.  
  933. //---------------------------------------------------------------------------
  934. // Name.......: ActionLogout
  935. // Purpose....: Handle BTN_LOGOUT command.
  936. //---------------------------------------------------------------------------
  937. static void ActionLogout(HWND hWnd)
  938.     { BOOL res;
  939.     res = LogoutFtp(hFtp);
  940.       ShowResult(hWnd,res);
  941.     }
  942.  
  943. //---------------------------------------------------------------------------
  944. // Name.......: ActionMakeDir
  945. // Purpose....: Handle BTN_MAKEDIR command.
  946. //---------------------------------------------------------------------------
  947. static void ActionMakeDir(HWND hWnd)
  948.     { char *PathName;
  949.       BOOL res;
  950.  
  951.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  952.  
  953.     if(PathName)
  954.         {
  955.         res = MakeDirFtp(hFtp,PathName);
  956.         ShowResult(hWnd,res);
  957.         FreeStringParam(PathName);
  958.         }
  959.     }
  960.  
  961. //---------------------------------------------------------------------------
  962. // Name.......: ActionMode
  963. // Purpose....: Handle BTN_MODE command.
  964. //---------------------------------------------------------------------------
  965. static void ActionMode(HWND hWnd)
  966.     { BOOL res;
  967.       FTP_TRANSFER_MODE Mode;
  968.       char *ModeName;
  969.  
  970.  
  971.     ModeName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  972.     if(ModeName)
  973.         {
  974.         if( stricmp(ModeName,"FTP_STREAM")==0 ||
  975.             stricmp(ModeName,"STREAM")==0)
  976.             {
  977.             Mode = FTP_STREAM;
  978.             }
  979.         else if( stricmp(ModeName,"FTP_BLOCK") == 0 ||
  980.                  stricmp(ModeName,"BLOCK") == 0)
  981.             {
  982.             Mode = FTP_BLOCK;
  983.             }
  984.         else if( stricmp(ModeName,"FTP_COMPRESSED") == 0 ||
  985.                  stricmp(ModeName,"COMPRESSED") == 0)
  986.             {
  987.             Mode = FTP_COMPRESSED;
  988.             }
  989.         else
  990.             {
  991.             SetDlgItemText(hWnd,STC_STATUS,"Enter: stream,block or compressed");
  992.             FreeStringParam(ModeName);
  993.             return;
  994.             }
  995.  
  996.  
  997.     res = ModeFtp(hFtp,Mode);
  998.     ShowResult(hWnd,res);
  999.     FreeStringParam(ModeName);
  1000.         }
  1001.     }
  1002.  
  1003. //---------------------------------------------------------------------------
  1004. // Name.......: ActionNameList
  1005. // Purpose....: Handle BTN_NAMELIST command.
  1006. //---------------------------------------------------------------------------
  1007. static void ActionNameList(HWND hWnd)
  1008.     { char *PathName;
  1009.       BOOL res;
  1010.  
  1011.     PathName = GetStringParam(hWnd,EDC_PARAM1,FALSE);
  1012.  
  1013.     if(PathName)
  1014.         {
  1015.         if(StartReceive(hWnd,NULL))
  1016.             {
  1017.             res = NameListFtp(hFtp,PathName);
  1018.             if(!res)
  1019.                 {
  1020.                 AbortRecv();
  1021.                 }
  1022.                ShowResult(hWnd,res);
  1023.             }
  1024.         FreeStringParam(PathName);
  1025.         }
  1026.     }
  1027.  
  1028. //---------------------------------------------------------------------------
  1029. // Name.......: ActionNoop
  1030. // Purpose....: Handle BTN_NOOP command.
  1031. //---------------------------------------------------------------------------
  1032. static void ActionNoop(HWND hWnd)
  1033.     { BOOL res;
  1034.     res = NoopFtp(hFtp);
  1035.     ShowResult(hWnd,res);
  1036.     }
  1037.  
  1038. //---------------------------------------------------------------------------
  1039. // Name.......: ActionPassive
  1040. // Purpose....: Handle BTN_PASSIVE command.
  1041. //---------------------------------------------------------------------------
  1042. static void ActionPassive(HWND hWnd)
  1043.     { BOOL res;
  1044.     res = PassiveFtp(hFtp);
  1045.     ShowResult(hWnd,res);
  1046.     }
  1047.  
  1048. //---------------------------------------------------------------------------
  1049. // Name.......: ActionPort
  1050. // Purpose....: Handle BTN_PORT command.
  1051. //---------------------------------------------------------------------------
  1052. static void ActionPort(HWND hWnd)
  1053.     { char *HostPort;
  1054.       BOOL res;
  1055.     HostPort = GetStringParam(hWnd,EDC_PARAM1,FALSE);
  1056.  
  1057.     if(HostPort)
  1058.         {
  1059.         res = PortFtp(hFtp,HostPort);
  1060.         ShowResult(hWnd,res);
  1061.         FreeStringParam(HostPort);
  1062.         }
  1063.     }
  1064.  
  1065. //---------------------------------------------------------------------------
  1066. // Name.......:ActionPrintWorkingDir
  1067. // Purpose....:Handle BTN_PRINTWORKINGDIR command.
  1068. //---------------------------------------------------------------------------
  1069. static void ActionPrintWorkingDir(HWND hWnd)
  1070.     { BOOL res;
  1071.     res = PrintWorkingDirFtp(hFtp);
  1072.     ShowResult(hWnd,res);
  1073.     }
  1074.  
  1075. //---------------------------------------------------------------------------
  1076. // Name.......:ActionReinitialize
  1077. // Purpose....:Handle BTN_REINITIALIZE command.
  1078. //---------------------------------------------------------------------------
  1079. static void ActionReinitialize(HWND hWnd)
  1080.     { BOOL res;
  1081.  
  1082.     res = ReinitializeFtp(hFtp);
  1083.       ShowResult(hWnd,res);
  1084.     }
  1085.  
  1086. //---------------------------------------------------------------------------
  1087. // Name.......:ActionRemoveDir
  1088. // Purpose....:Handle BTN_REMOVEDIR command.
  1089. //---------------------------------------------------------------------------
  1090. static void ActionRemoveDir(HWND hWnd)
  1091.     { char *PathName;
  1092.       BOOL res;
  1093.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1094.  
  1095.     if(PathName)
  1096.         {
  1097.         res = RemoveDirFtp(hFtp,PathName);
  1098.         ShowResult(hWnd,res);
  1099.         FreeStringParam(PathName);
  1100.         }
  1101.     }
  1102.  
  1103. //---------------------------------------------------------------------------
  1104. // Name.......:ActionRename
  1105. // Purpose....:Handle BTN_RENAME command.
  1106. //---------------------------------------------------------------------------
  1107. static void ActionRename(HWND hWnd)
  1108.     { char *FromPathName;
  1109.       char *ToPathName;
  1110.       BOOL res;
  1111.  
  1112.     ToPathName      = GetStringParam(hWnd,EDC_PARAM2,TRUE);
  1113.     FromPathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1114.  
  1115.     if(FromPathName && ToPathName)
  1116.         {
  1117.         res = RenameFtp(hFtp,FromPathName,ToPathName);
  1118.         ShowResult(hWnd,res);
  1119.         }
  1120.  
  1121.     FreeStringParam(FromPathName);
  1122.     FreeStringParam(ToPathName);
  1123.     }
  1124.  
  1125. //---------------------------------------------------------------------------
  1126. // Name.......:ActionRestart
  1127. // Purpose....:Handle BTN_RESTART command.
  1128. //---------------------------------------------------------------------------
  1129. static void ActionRestart(HWND hWnd)
  1130.     { char *Marker;
  1131.       BOOL res;
  1132.  
  1133.     Marker = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1134.  
  1135.     if(Marker)
  1136.         {
  1137.         res = RestartFtp(hFtp,Marker);
  1138.         ShowResult(hWnd,res);
  1139.         FreeStringParam(Marker);
  1140.         }
  1141.  
  1142.     }
  1143. //---------------------------------------------------------------------------
  1144. // Name.......:ActionRetrieve
  1145. // Purpose....:Handle BTN_RETRIEVE command.
  1146. //---------------------------------------------------------------------------
  1147. static void ActionRetrieve(HWND hWnd)
  1148.     { char *PathName;
  1149.       char *LocalName;
  1150.       BOOL res;
  1151.  
  1152.     PathName  = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1153.     LocalName = GetStringParam(hWnd,EDC_PARAM2,FALSE);
  1154.  
  1155.     if(PathName)
  1156.         {
  1157.         if(StartReceive(hWnd,*LocalName ? LocalName : NULL))
  1158.             {
  1159.             res = RetrieveFtp(hFtp,PathName);
  1160.             if(!res)
  1161.                 {
  1162.                 AbortRecv();
  1163.                 }
  1164.             ShowResult(hWnd,res);
  1165.             }
  1166.             FreeStringParam(PathName);
  1167.         }
  1168.  
  1169.     FreeStringParam(LocalName);
  1170.     }
  1171.  
  1172.  
  1173. //---------------------------------------------------------------------------
  1174. // Name.......:ActionSite
  1175. // Purpose....:Handle BTN_SITE command.
  1176. //---------------------------------------------------------------------------
  1177. static void ActionSite(HWND hWnd)
  1178.     { char *Desc;
  1179.       BOOL res;
  1180.  
  1181.     Desc = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1182.  
  1183.     if(Desc)
  1184.         {
  1185.         res = SiteFtp(hFtp,Desc);
  1186.         ShowResult(hWnd,res);
  1187.         FreeStringParam(Desc);
  1188.         }
  1189.     }
  1190.  
  1191. //---------------------------------------------------------------------------
  1192. // Name.......:ActionStatus
  1193. // Purpose....:Handle BTN_STATUS command.
  1194. //---------------------------------------------------------------------------
  1195. static void ActionStatus(HWND hWnd)
  1196.     { char *PathName;
  1197.       BOOL res;
  1198.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1199.  
  1200.     if(PathName)
  1201.         {
  1202.         res = StatusFtp(hFtp,PathName);
  1203.         ShowResult(hWnd,res);
  1204.         FreeStringParam(PathName);
  1205.         }
  1206.     }
  1207.  
  1208. //---------------------------------------------------------------------------
  1209. // Name.......:ActionStore
  1210. // Purpose....:Handle BTN_STORE command.
  1211. //---------------------------------------------------------------------------
  1212. static void ActionStore(HWND hWnd)
  1213.     { char *PathName;
  1214.       char *LocalName;
  1215.       BOOL res;
  1216.  
  1217.     LocalName = GetStringParam(hWnd,EDC_PARAM2,TRUE);
  1218.     PathName  = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1219.  
  1220.     if(PathName && LocalName)
  1221.         {
  1222.         if(StartSend(hWnd,LocalName))
  1223.             {
  1224.             res = StoreFtp(hFtp,PathName);
  1225.             if(!res)
  1226.                 {
  1227.                 AbortSend();
  1228.                 }
  1229.             ShowResult(hWnd,res);
  1230.             }
  1231.         }
  1232.  
  1233.     FreeStringParam(PathName);
  1234.     FreeStringParam(LocalName);
  1235.     }
  1236.  
  1237. //---------------------------------------------------------------------------
  1238. // Name.......:ActionStoreUnique
  1239. // Purpose....:Handle BTN_STOREUNIQUE command.
  1240. //---------------------------------------------------------------------------
  1241. static void ActionStoreUnique(HWND hWnd)
  1242.     { char *PathName;
  1243.       char *LocalName;
  1244.       BOOL res;
  1245.  
  1246.     LocalName = GetStringParam(hWnd,EDC_PARAM2,TRUE);
  1247.     PathName  = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1248.  
  1249.     if(PathName && LocalName)
  1250.         {
  1251.         if(StartSend(hWnd,LocalName))
  1252.             {
  1253.             res = StoreUniqueFtp(hFtp,PathName);
  1254.             if(!res)
  1255.                 {
  1256.                 AbortSend();
  1257.                 }
  1258.             ShowResult(hWnd,res);
  1259.             }
  1260.         }
  1261.  
  1262.     FreeStringParam(PathName);
  1263.     FreeStringParam(LocalName);
  1264.  
  1265.     }
  1266.  
  1267. //---------------------------------------------------------------------------
  1268. // Name.......:ActionStructMount
  1269. // Purpose....:Handle BTN_STRUCTMOUNT command.
  1270. //---------------------------------------------------------------------------
  1271. static void ActionStructMount(HWND hWnd)
  1272.     { char *PathName;
  1273.       BOOL res;
  1274.  
  1275.     PathName = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1276.  
  1277.     if(PathName)
  1278.         {
  1279.         res = StructMountFtp(hFtp,PathName);
  1280.         ShowResult(hWnd,res);
  1281.         FreeStringParam(PathName);
  1282.         }
  1283.     }
  1284.  
  1285. //---------------------------------------------------------------------------
  1286. // Name.......:ActionSystem
  1287. // Purpose....:Handle BTN_SYSTEM command.
  1288. //---------------------------------------------------------------------------
  1289. static void ActionSystem(HWND hWnd)
  1290.     { BOOL res;
  1291.     res = SystemFtp(hFtp);
  1292.     ShowResult(hWnd,res);
  1293.     }
  1294.  
  1295. //---------------------------------------------------------------------------
  1296. // Name.......:ActionType
  1297. // Purpose....:Handle BTN_TYPE command.
  1298. //---------------------------------------------------------------------------
  1299. static void ActionType(HWND hWnd)
  1300.     { char *RepType;
  1301.       BOOL res;
  1302.     RepType = GetStringParam(hWnd,EDC_PARAM1,TRUE);
  1303.  
  1304.     if(RepType)
  1305.         {
  1306.         res = TypeFtp(hFtp,RepType);
  1307.         ShowResult(hWnd,res);
  1308.         FreeStringParam(RepType);
  1309.         }
  1310.     }
  1311.  
  1312. //---------------------------------------------------------------------------
  1313. // Name.......:ActionClearLog
  1314. // Purpose....:Handle BTN_CLEARLOG command.
  1315. //---------------------------------------------------------------------------
  1316. static void ActionClearLog(HWND hWnd)
  1317.     {
  1318.     SendDlgItemMessage(hWnd,LBC_LOG,LB_RESETCONTENT,0,0);
  1319.     }
  1320.  
  1321. //---------------------------------------------------------------------------
  1322. // Name.......:ActionClearReplies
  1323. // Purpose....:Handle BTN_CLEARREPLIES command.
  1324. //---------------------------------------------------------------------------
  1325. static void ActionClearReplies(HWND hWnd)
  1326.     {
  1327.     SendDlgItemMessage(hWnd,LBC_REPLIES,LB_RESETCONTENT,0,0);
  1328.     }
  1329.  
  1330. //---------------------------------------------------------------------------
  1331. // Name.......:ShowParams
  1332. // Purpose....:Show parameters for given button.
  1333. //---------------------------------------------------------------------------
  1334. static void ShowParams(HWND hOurWnd,HWND hCtlWnd)
  1335.     { int id;
  1336.      char *p1;
  1337.      char *p2;
  1338.  
  1339.     id = GetDlgCtrlID(hCtlWnd);
  1340.     switch(id)
  1341.         {
  1342.         // No param
  1343.         case BTN_CHDIRUP:
  1344.         case BTN_CLOSEDATA:
  1345.         case BTN_LASTCOMMAND:
  1346.         case BTN_NOOP:
  1347.         case BTN_PASSIVE:
  1348.         case BTN_PRINTWORKINGDIR:
  1349.         case BTN_SYSTEM:
  1350.               p1=p2="";
  1351.               break;
  1352.         case BTN_APPE:
  1353.         case BTN_STORE:
  1354.         case BTN_STOREUNIQUE:
  1355.         case BTN_RETRIEVE:
  1356.             p1="PathName";
  1357.             p2="LocalName";
  1358.             break;
  1359.         // pathname
  1360.         case BTN_CHDIR:
  1361.         case BTN_DELETE:
  1362.         case BTN_LIST:
  1363.         case BTN_MAKEDIR:
  1364.         case BTN_NAMELIST:
  1365.         case BTN_REMOVEDIR:
  1366.         case BTN_STATUS:
  1367.         case BTN_STRUCTMOUNT:
  1368.             p1="PathName";
  1369.             p2="";
  1370.             break;
  1371.  
  1372.         // command
  1373.         case BTN_COMMAND:
  1374.         case BTN_HELP:
  1375.             p1="Command";
  1376.             p2="";
  1377.             break;
  1378.  
  1379.         case BTN_ALLOCATE:
  1380.             p1="FileSize";
  1381.             p2="RecordSize";
  1382.             break;
  1383.         case BTN_FILESTRUCT:
  1384.             p1="Structure";
  1385.             p2="";
  1386.             break;
  1387.  
  1388.         case BTN_MODE:
  1389.             p1="XferMode";
  1390.             p2="";
  1391.             break;
  1392.  
  1393.         case BTN_PORT:
  1394.             p1="HostPort";
  1395.             p2="";
  1396.             break;
  1397.         case BTN_RENAME:
  1398.             p1="FromName";
  1399.             p2="ToName";
  1400.             break;
  1401.  
  1402.         case BTN_RESTART:
  1403.             p1="Marker";
  1404.             p2="";
  1405.             break;
  1406.  
  1407.         case BTN_SITE:
  1408.             p1="Desc";
  1409.             p2="";
  1410.             break;
  1411.  
  1412.         case BTN_TYPE:
  1413.             p1="RepType";
  1414.             p2="";
  1415.             break;
  1416.  
  1417.  
  1418.         default: return;
  1419.  
  1420.         }
  1421.         SetDlgItemText(hOurWnd,STC_PARAM1,p1);
  1422.         SetDlgItemText(hOurWnd,STC_PARAM2,p2);
  1423.     }
  1424.  
  1425. //---------------------------------------------------------------------------
  1426. // Name.......:CanClose
  1427. // Purpose....:Return TRUE if ok to close
  1428. //---------------------------------------------------------------------------
  1429. static BOOL CanClose(HWND hWnd)
  1430.     { int res;
  1431.  
  1432.     // User wants to shut down. If still logged on (hFtp != NULL)
  1433.     // encourge user to log off first.
  1434.     if(!hFtp)
  1435.         {
  1436.         return TRUE;
  1437.         }
  1438.  
  1439.     res = MessageBox(hWnd,
  1440.                        "You are still logged on.\n"
  1441.                         "You should logout before closing.\n"
  1442.                         "Would you like to logout now?",
  1443.                         "FTPTest",
  1444.                         MB_YESNOCANCEL | MB_ICONQUESTION);
  1445.     switch(res)
  1446.         {
  1447.         case IDYES:    ClosePending = TRUE;
  1448.                        LogoutFtp(hFtp);
  1449.                        return FALSE;
  1450.         case IDNO:     return TRUE;
  1451.         case IDCANCEL: return FALSE;
  1452.         }
  1453.  
  1454.     return TRUE;
  1455.     }
  1456.  
  1457. //---------------------------------------------------------------------------
  1458. // Name.......:ReadIniFile
  1459. // Purpose....:Reads default login values.
  1460. //---------------------------------------------------------------------------
  1461. static void ReadIniFile(HWND hWnd)
  1462.     { char *Name;
  1463.  
  1464.     Name = malloc(256);
  1465.     if(!Name)
  1466.         {
  1467.         return;
  1468.         }
  1469.  
  1470.     GetPrivateProfileString("FTPTest",
  1471.                             "RemoteHost",
  1472.                             "",
  1473.                             Name,
  1474.                             256,
  1475.                             "FTPTEST.INI");
  1476.     SendDlgItemMessage(hWnd,EDC_HOST,WM_SETTEXT,0,(LPARAM)(LPCSTR)Name);
  1477.  
  1478.     GetPrivateProfileString("FTPTest",
  1479.                             "User",
  1480.                             "",
  1481.                             Name,
  1482.                             256,
  1483.                             "FTPTEST.INI");
  1484.     SendDlgItemMessage(hWnd,EDC_USER,WM_SETTEXT,0,(LPARAM)(LPCSTR)Name);
  1485.  
  1486.     GetPrivateProfileString("FTPTest",
  1487.                             "LicenseKey",
  1488.                             "",
  1489.                             Name,
  1490.                             256,
  1491.                             "FTPTEST.INI");
  1492.     SendDlgItemMessage(hWnd,EDC_LICENSE,WM_SETTEXT,0,(LPARAM)(LPCSTR)Name);
  1493.  
  1494.     GetPrivateProfileString("FTPTest",
  1495.                             "Account",
  1496.                             "",
  1497.                             Name,
  1498.                             256,
  1499.                             "FTPTEST.INI");
  1500.     SendDlgItemMessage(hWnd,EDC_ACCOUNT,WM_SETTEXT,0,(LPARAM)(LPCSTR)Name);
  1501.  
  1502.  
  1503.     free(Name);
  1504.     }
  1505.  
  1506. //---------------------------------------------------------------------------
  1507. // Name.......:WriteIniFile
  1508. // Purpose....:Writes current login values.
  1509. //---------------------------------------------------------------------------
  1510. static void WriteIniFile(HWND hWnd)
  1511.     { char *Name;
  1512.  
  1513.     Name = GetStringParam(hWnd,EDC_HOST,FALSE);
  1514.     if(Name)
  1515.         {
  1516.         WritePrivateProfileString("FTPTest",
  1517.                                   "RemoteHost",
  1518.                                   Name,
  1519.                                   "FTPTEST.INI");
  1520.         FreeStringParam(Name);
  1521.         }
  1522.  
  1523.     Name = GetStringParam(hWnd,EDC_USER,FALSE);
  1524.     if(Name)
  1525.         {
  1526.         WritePrivateProfileString("FTPTest",
  1527.                                   "User",
  1528.                                   Name,
  1529.                                   "FTPTEST.INI");
  1530.         FreeStringParam(Name);
  1531.         }
  1532.  
  1533.     Name = GetStringParam(hWnd,EDC_LICENSE,FALSE);
  1534.     if(Name)
  1535.         {
  1536.         WritePrivateProfileString("FTPTest",
  1537.                                   "LicenseKey",
  1538.                                   Name,
  1539.                                   "FTPTEST.INI");
  1540.         FreeStringParam(Name);
  1541.         }
  1542.  
  1543.     Name = GetStringParam(hWnd,EDC_ACCOUNT,FALSE);
  1544.     if(Name)
  1545.         {
  1546.         WritePrivateProfileString("FTPTest",
  1547.                                   "Account",
  1548.                                   Name,
  1549.                                   "FTPTEST.INI");
  1550.         FreeStringParam(Name);
  1551.         }
  1552.  
  1553.     }
  1554.  
  1555.  
  1556.  
  1557. //---------------------------------------------------------------------------
  1558. // Name.......:MainWndProc
  1559. // Purpose....:Main windows procedure for FTPTest.
  1560. //---------------------------------------------------------------------------
  1561. static DLGPROC lpfnMainWndProc;
  1562.  
  1563. BOOL CALLBACK MainWndProc(HWND hWnd, UINT wMessage, WPARAM wParam, LPARAM lParam)
  1564.     {
  1565.     static HWND hCursorWnd;
  1566.     HDC hDc;
  1567.     PAINTSTRUCT ps;
  1568.  
  1569.     switch(wMessage)
  1570.            {
  1571.  
  1572.            case WM_INITDIALOG :
  1573.             // Get default login values, init status line, put
  1574.             // cursor in HostName edit control
  1575.             ReadIniFile(hWnd);
  1576.             SetDlgItemText(hWnd,STC_STATUS,"FTPTest Version 1.0");
  1577.             SetFocus(GetDlgItem(hWnd,EDC_HOST));
  1578.             SendDlgItemMessage(hWnd,LBC_LOG , LB_SETHORIZONTALEXTENT,
  1579.                                800, 0L);
  1580.             SendDlgItemMessage(hWnd,LBC_REPLIES , LB_SETHORIZONTALEXTENT,
  1581.                                800, 0L);
  1582.     
  1583.             return TRUE;
  1584.  
  1585.            case WM_COMMAND :
  1586.          // Dispatch a button click.
  1587.          switch(wParam)
  1588.             {
  1589.             case BTN_ABORT:             ActionAbort(hWnd);                break;
  1590.             case BTN_ALLOCATE:            ActionAllocate(hWnd);            break;
  1591.             case BTN_APPE:                ActionAppe(hWnd);                break;
  1592.             case BTN_CHDIR:                ActionChDir(hWnd);                break;
  1593.             case BTN_CHDIRUP:            ActionChDirUp(hWnd);            break;
  1594.             case BTN_CLOSE:                ActionClose(hWnd);                break;
  1595.             case BTN_CLOSEDATA:            ActionCloseData(hWnd);            break;
  1596.             case BTN_COMMAND:            ActionCommand(hWnd);            break;
  1597.             case BTN_DELETE:            ActionDelete(hWnd);                break;
  1598.             case BTN_FILESTRUCT:        ActionFileStruct(hWnd);            break;
  1599.             case BTN_HELP:                ActionHelp(hWnd);                break;
  1600.             case BTN_LASTCOMMAND:        ActionLastCommand(hWnd);        break;
  1601.             case BTN_LIST:                ActionList(hWnd);                break;
  1602.             case BTN_LOGINHOST:            ActionLoginHost(hWnd);            break;
  1603.             case BTN_LOGOUT:            ActionLogout(hWnd);                break;
  1604.             case BTN_MAKEDIR:            ActionMakeDir(hWnd);            break;
  1605.             case BTN_MODE:                ActionMode(hWnd);                break;
  1606.             case BTN_NAMELIST:            ActionNameList(hWnd);            break;
  1607.             case BTN_NOOP:                ActionNoop(hWnd);                break;
  1608.             case BTN_PASSIVE:            ActionPassive(hWnd);            break;
  1609.             case BTN_PORT:                ActionPort(hWnd);                break;
  1610.             case BTN_PRINTWORKINGDIR:    ActionPrintWorkingDir(hWnd);    break;
  1611.             case BTN_REINITIALIZE:        ActionReinitialize(hWnd);        break;
  1612.             case BTN_REMOVEDIR:            ActionRemoveDir(hWnd);            break;
  1613.             case BTN_RENAME:            ActionRename(hWnd);                break;
  1614.             case BTN_RESTART:            ActionRestart(hWnd);            break;
  1615.             case BTN_RETRIEVE:            ActionRetrieve(hWnd);            break;
  1616.             case BTN_SITE:                ActionSite(hWnd);                break;
  1617.             case BTN_STATUS:            ActionStatus(hWnd);                break;
  1618.             case BTN_STORE:                ActionStore(hWnd);                break;
  1619.             case BTN_STOREUNIQUE:        ActionStoreUnique(hWnd);        break;
  1620.             case BTN_STRUCTMOUNT:        ActionStructMount(hWnd);        break;
  1621.             case BTN_SYSTEM:            ActionSystem(hWnd);                break;
  1622.             case BTN_TYPE:                ActionType(hWnd);                break;
  1623.  
  1624.             case BTN_CLEARLOG:          ActionClearLog(hWnd);            break;
  1625.             case BTN_CLEARREPLIES:         ActionClearReplies(hWnd);        break;
  1626.  
  1627.             }
  1628.  
  1629.           break;
  1630.  
  1631.        case WM_MOUSEACTIVATE:
  1632.           // If button in Data group clicked with left or
  1633.           // right button, show the parameters for that button.
  1634.           ShowParams(hWnd,hCursorWnd);
  1635.           if(HIWORD(lParam) == WM_RBUTTONDOWN)
  1636.               {
  1637.             return MA_NOACTIVATE;
  1638.               }
  1639.           else
  1640.             {
  1641.             return MA_ACTIVATE;
  1642.             }
  1643.           // break;
  1644.  
  1645.        case WM_SETCURSOR:
  1646.           // Keep track of which control the mouse is over.
  1647.           hCursorWnd = (HWND)wParam;
  1648.           break;
  1649.        case WM_SYSCOMMAND :
  1650.  
  1651.  
  1652.           switch(wParam & 0xFFF0)
  1653.           {
  1654.               case SC_CLOSE :
  1655.                 // Save login information, see if ok to quit.
  1656.                 WriteIniFile(hWnd);
  1657.                 if(CanClose(hWnd))
  1658.                     {
  1659.                     PostQuitMessage(0);
  1660.                     return TRUE;
  1661.                     }
  1662.                 else
  1663.                     {
  1664.                     return FALSE;
  1665.                     }
  1666.           }
  1667.           break;
  1668.  
  1669.  
  1670.        case WM_PAINT:
  1671.           // Since we don't register a class, we draw our own
  1672.           // icon when we are minimized.
  1673.           if(IsIconic(hWnd))
  1674.               {
  1675.             hDc = BeginPaint(hWnd,&ps);
  1676.             DrawIcon(hDc,0,0,hOurIcon);
  1677.             EndPaint(hWnd,&ps);
  1678.               }
  1679.             break;
  1680.        case WM_ERASEBKGND:
  1681.           // The icon contains some transparent pixels. If we
  1682.           // just ignore the erase background message, then
  1683.           // DefDlgProc paints the background white. If we
  1684.           // swallow the message, then garbage is left in the
  1685.           // transparent areas when another window hides the
  1686.           // icon and is then moved. Microsoft solved this problem
  1687.           // years ago. The solution is in DefWindowProc, case
  1688.           // WM_ICONERASEBKGND. So let them do the work!
  1689.           if(IsIconic(hWnd))
  1690.               {
  1691.              return (BOOL)DefWindowProc(hWnd,WM_ICONERASEBKGND,wParam,lParam);
  1692.             }
  1693.           break;
  1694.  
  1695.        case WM_DESTROY :
  1696.           PostQuitMessage(0);
  1697.           break;
  1698.  
  1699.        default :
  1700.           return FALSE;
  1701.           }
  1702.    return FALSE;
  1703. }
  1704.  
  1705. //---------------------------------------------------------------------------
  1706. // Name.......:WinMain
  1707. // Purpose....:Create application dialog and dispatch messages.
  1708. //---------------------------------------------------------------------------
  1709. int PASCAL WinMain(HINSTANCE hInstance,         // Application Instance Handle
  1710.                    HINSTANCE hPrevInstance,     // Previous Instance Handle
  1711.                    LPSTR  lpszCmdLine,          // Pointer to Command Line
  1712.                    int    nCmdShow)             // Show Window Option
  1713. {
  1714.    static char szAppName[] = "FTPtest";
  1715.    HINSTANCE hInst;
  1716.  
  1717.  
  1718.    MSG      msg;
  1719.    HWND     hWndMain;
  1720.  
  1721.    hInst = hInstance;
  1722.    hOurIcon = LoadIcon(hInst,szAppName);
  1723.  
  1724.    lpfnMainWndProc    = (DLGPROC)MakeProcInstance((FARPROC)MainWndProc,hInst);
  1725.  
  1726.    lpfnConnectEvent = (CONNECTEVENT)MakeProcInstance((FARPROC)ConnectEvent,
  1727.                                                           hInst);
  1728.    lpfnLogEvent     = (LOGEVENT)MakeProcInstance((FARPROC)LogEvent,hInst);
  1729.    lpfnRecvEvent     = (RECVEVENT)MakeProcInstance((FARPROC)RecvEvent,hInst);
  1730.    lpfnReplyEvent     = (REPLYEVENT)MakeProcInstance((FARPROC)ReplyEvent,hInst);
  1731.    lpfnSendEvent     = (SENDEVENT)MakeProcInstance((FARPROC)SendEvent,hInst);
  1732.  
  1733.    if(!(hWndMain = CreateDialog(hInst,
  1734.                    szAppName,
  1735.                    NULL,
  1736.                    lpfnMainWndProc)))
  1737.    {
  1738.         MessageBox(NULL, "Unable to display main dialog", "System Error", MB_OK);
  1739.         return FALSE;
  1740.    }
  1741.  
  1742.  
  1743.    ShowWindow(hWndMain, nCmdShow);
  1744.    UpdateWindow(hWndMain);
  1745.  
  1746.  
  1747.    while(GetMessage(&msg, NULL, 0, 0))
  1748.          if(!IsDialogMessage(hWndMain, &msg))
  1749.           {
  1750.              TranslateMessage(&msg);
  1751.              DispatchMessage(&msg);
  1752.           }
  1753.  
  1754.    FreeProcInstance((FARPROC)lpfnMainWndProc);
  1755.    FreeProcInstance((FARPROC)lpfnConnectEvent);
  1756.    FreeProcInstance((FARPROC)lpfnLogEvent);
  1757.    FreeProcInstance((FARPROC)lpfnRecvEvent);
  1758.    FreeProcInstance((FARPROC)lpfnReplyEvent);
  1759.    FreeProcInstance((FARPROC)lpfnSendEvent);
  1760.  
  1761.    return msg.wParam;
  1762. }
  1763.